﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class newUser : Window
    {
        private static List<Canvas> CanvasList;
        int i;
        System.Windows.Threading.DispatcherTimer dispatcherTimer;

        public newUser()
        {
            InitializeComponent();

            i = 0;
            CanvasList = new List<Canvas>();
            CanvasList.Add(cvsMainWindow);
            CanvasList.Add(cvsUserAgreement);
            CanvasList.Add(cvsAccountName);
            CanvasList[0].Visibility = Visibility.Visible;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {

            if (i < 2)
            {
                CanvasList[i].Visibility = Visibility.Hidden;
                i = i + 1;
                CanvasList[i].Visibility = Visibility.Visible;
            }
            if (i == 2)
            {
                btnOk.Visibility = Visibility.Visible;
                btnNext.Visibility = Visibility.Hidden;
            }

        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (i == 0)
            {
                formController.ShowForm(typeof(mainWindow)); 
            }

            if (i > 0)
            {
                CanvasList[i].Visibility = Visibility.Hidden;
                i = i - 1;
                CanvasList[i].Visibility = Visibility.Visible;
                btnOk.Visibility = Visibility.Hidden;
                btnNext.Visibility = Visibility.Visible;
            }
        }

        private void tbxCancel_Click(object sender, RoutedEventArgs e)
        {
            formController.ShowForm(typeof(mainWindow)); 
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            formController.user.username = tbxUserName.Text;
            formController.user.password = tbxPassword.Text;
            formController.user.firstname = tbxFirstName.Text;
            formController.user.lastname = tbxLastName.Text;
            formController.user.phone = tbxPhone.Text;
            formController.user.email = tbxEmail.Text;


            formController.CreateUser(tbxUserName.Text, tbxPassword.Text, tbxFirstName.Text, tbxLastName.Text, tbxEmail.Text, tbxPhone.Text);       
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (formController.newProtocol == true)
            {
                switch (formController.receivedProtocol)
                {

                    #region Get User List

                    case "UC":
                        {
                            if (formController.receivedMessage == "OK")
                            {
                                dispatcherTimer.Stop();
                                formController.ShowForm(typeof(saunaMain));
                            }
                            else
                            {
                                if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }
                            }
                        }
                        break;

                    #endregion

                    #region User Exists

                    case "UE":
                        {
                            if (formController.errorMessage != "") { MessageBox.Show(formController.errorMessage); }
                        }
                        break;

                    #endregion

                    default:

                        break;
                }

                formController.newProtocol = false;
            }


        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            dispatcherTimer.Start();
        }
    }
}
